package com.inetools.gwetons.client;

import com.google.gwt.dom.client.Document;
import com.google.gwt.dom.client.Style.Unit;
import com.google.gwt.event.logical.shared.SelectionEvent;
import com.google.gwt.event.logical.shared.SelectionHandler;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.DecoratedTabPanel;
import com.google.gwt.user.client.ui.TabLayoutPanel;

/**
 * Provide appropriate TabPanel type via TabPanelProviderService interface based on document.compatMode value: 
 * either TabLayoutPanel for Standard mode or DecoratedTabPanel for Quirks mode.
 * Since TabPanelProviderService interface provides common methods between TabLayoutPanel and 
 * DecoratedTabPanel, using TabPanelProviderService interface object eliminates the consideration for 
 * which type the underneath tab panel class object. However, be aware that there are different behaviors 
 * between TabLayoutPanel and DecoratedTabPanel especially regarding style.
 * @author ay
 */
public class TabPanelProviderImpl extends Composite {
	class StandardModeTabPanel extends TabLayoutPanel implements TabPanelProviderService {
		public StandardModeTabPanel() {
			super( 1.5, Unit.EM);
		}
	} // class StandardModeTabPanel extends TabLayoutPanel implements TabPanelProviderService
	
	class QuirksModeTabPanel extends DecoratedTabPanel implements TabPanelProviderService {
		protected int selectedIndex = -1;
		
		public QuirksModeTabPanel() {
			super();
			this.addSelectionHandler( new SelectionHandler<Integer>() {
				@Override
				public void onSelection(SelectionEvent<Integer> event) {
					selectedIndex = event.getSelectedItem().intValue();
				}
			});
		}

		@Override
		public int getSelectedIndex() {
			return selectedIndex;
		}
	} // class StandardModeTabPanel extends TabLayoutPanel implements TabPanelProviderService
	
	protected TabPanelProviderService tabPanelProviderService;
	public TabPanelProviderService getTabPanelProviderService() {
		return tabPanelProviderService;
	}

	public TabPanelProviderImpl() {
		if ( "BackCompat".equals( Document.get().getCompatMode())) {
			QuirksModeTabPanel tabPanel = new QuirksModeTabPanel();
			initWidget( tabPanel);
			tabPanelProviderService = tabPanel;
		}
		else {
			StandardModeTabPanel tabPanel = new StandardModeTabPanel();
			initWidget( tabPanel);
			tabPanelProviderService = tabPanel;
		}
	}
	
}
